package com.dhj.handler;

import com.alibaba.fastjson.JSONObject;
import com.dhj.baseBean.ResponseResult;
import com.dhj.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author DaHuaJia
 * @Description 认证异常处理
 * @Date 2022-04-04 11:17:53
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {
        // 定义认证异常消息
        ResponseResult result = new ResponseResult(HttpStatus.UNAUTHORIZED.value(), "用户认证失败，请重新登录。");
        // 封装数据到response
        WebUtils.renderString(response, JSONObject.toJSONString(result));
    }

}
